package com.wzcl.app.service.impl;

import com.wzcl.app.dao.model.EssayCourseCatalog;
import com.wzcl.app.dao.model.WritingMethodBase;
import com.wzcl.app.dao.repository.EssayCourseCatalogRepository;
import com.wzcl.app.dao.repository.WritingMethodBaseRepository;
import com.wzcl.app.model.common.RVO;
import com.wzcl.app.model.dto.WritingMethodBaseDto;
import com.wzcl.app.service.WritingMethodBaseService;
import com.wzcl.app.utils.CollectionUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author beingven
 * @Description TODO
 * @createTime 2020年12月23日 15:10:00
 */
@Service
public class WritingMethodBaseServiceImpl implements WritingMethodBaseService {
    @Resource
    private WritingMethodBaseRepository writingMethodBaseRepository;
    @Resource
    private EssayCourseCatalogRepository essayCourseCatalogRepository;

    @Override
    @Transactional
    public RVO createWritingMethodBase(WritingMethodBaseDto writingMethodBaseDto) {
        EssayCourseCatalog essayCourseCatalog = essayCourseCatalogRepository.findCatalogById(writingMethodBaseDto.getCatalogId());
        WritingMethodBase writingMethodBase = new WritingMethodBase();
        if (writingMethodBaseDto.getMindMappingList() != null){
            String mindMappingList = CollectionUtil.getStringFromList(writingMethodBaseDto.getMindMappingList(), "|");
            writingMethodBase.setMindMappingList(mindMappingList);
            writingMethodBase.setCourseCatalog(essayCourseCatalog);
        }
        if (writingMethodBaseDto.getMaterialList() != null){
            String materialList = CollectionUtil.getStringFromList(writingMethodBaseDto.getMaterialList(), "|");
            writingMethodBase.setMaterialList(materialList);
            writingMethodBase.setCourseCatalog(essayCourseCatalog);
        }
        if (writingMethodBaseDto.getWritingTipsList() != null){
            String writingTipsList = CollectionUtil.getStringFromList(writingMethodBaseDto.getWritingTipsList(), "|");
            writingMethodBase.setWritingTipsList(writingTipsList);
            writingMethodBase.setCourseCatalog(essayCourseCatalog);
        }
        //绑定章节
        WritingMethodBase save = writingMethodBaseRepository.save(writingMethodBase);
        if (save != null){
            essayCourseCatalogRepository.updateCatalogById(save.getId(), writingMethodBaseDto.getCatalogId());
        }
        return RVO.success("写作锦囊添加成功");
    }
}
